package com.majiang.community.mapper;

import com.majiang.community.model.Question;
import com.majiang.community.model.QuestionExample;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface QuestionMapper {

    /**
     * 新增发布问题描述
     * @param question
     */
    @Insert("INSERT INTO question(title,description,gmtCreate,gmtModified,creator,tags) values (#{title},#{description},#{gmtcreate},#{gmtmodified},#{creator},#{tags})")
    boolean create(Question question);

    /**
     * 查询所有的发布信息
     */
    @Select("SELECT * FROM question limit #{offset},#{size}")
    List<Question> list(@Param(value = "offset") Integer offset, @Param(value = "size") Integer size);

    /**
     * 计算总条数
     */
    @Select("SELECT COUNT(1) FROM question")
    Integer count();

    /**
     * 根据id查找相应问题，用户查看自己的问题
     * @param userId
     * @param offset
     * @param size
     * @return
     */
    @Select("SELECT * FROM question where creator = #{userId} limit #{offset},#{size}")
    List<Question> listByUserId(@Param("userId") Long userId,
                                @Param(value = "offset") Integer offset,
                                @Param(value = "size") Integer size);

    /**
     * 查询问题的总数
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(1) FROM question WHERE creator = #{userId}")
    Integer countByUserId(@Param("userId") Long userId);

    /**
     * 点击问题信息根据id跳转到指定页面
     * @param id
     * @return
     */
    @Select("SELECT * FROM question where id = #{id}")
    Question getById(@Param("id") Long id);

    /**
     * 执行问题更新操作
     * @param question
     */
    @Update("update question set title=#{title},description=#{description},gmtModified={gmtModified},tags=#{tags} where id=#{id}")
    int update(Question question);

    @Update("update question set viewCount=viewCount+1 where id=#{id}")
    void addViewCount(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    long countByExample(QuestionExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    int deleteByExample(QuestionExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    int deleteByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    int insert(Question record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    int insertSelective(Question record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    List<Question> selectByExample(QuestionExample example);

    Question selectByPrimaryKey(Long id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    int updateByExampleSelective(@Param("record") Question record, @Param("example") QuestionExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    int updateByExample(@Param("record") Question record, @Param("example") QuestionExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    int updateByPrimaryKeySelective(Question record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table QUESTION
     *
     * @mbg.generated Fri Aug 09 10:04:53 CST 2019
     */
    int updateByPrimaryKey(Question record);
}